/*
  Copyright 1996-2003
  Simon Whiteside

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  $Id: TreeNodeObjectEnumerator.java,v 1.7 2003/02/24 18:10:53 simkin_cvs Exp $
*/
package simkin;
import java.util.*;

/**
 * This class enumerates the elements in an TreeNodeObject 
 * 
 *
*/
public class TreeNodeObjectEnumerator implements ExecutableIterator{
    TreeNodeObject m_TreeNodeObject;
    String m_TagName="";
    int m_NodeNum=0;
    /**
     * Constructor - enumerates over every child node of this object
     */
    public TreeNodeObjectEnumerator(TreeNodeObject treeNodeObject){
	m_TreeNodeObject=treeNodeObject;
    }
    /**
     * Constructor - enumerates over children of this object with the matching tag
     */
    public TreeNodeObjectEnumerator(TreeNodeObject treeNodeObject,String tagName){
	m_TreeNodeObject=treeNodeObject;
	m_TagName=tagName;
	// first wind forward to the first occurrence of the tag
	findNextNode();
    }
    /**
     * returns the next object in the enumeration - or null if there are no more
     */
    public Object next(){
	Vector nodes=m_TreeNodeObject.getNode().getItems();
	TreeNodeObject ret=null;
	if (nodes!=null && m_NodeNum<nodes.size()){
	    TreeNode node=(TreeNode)nodes.elementAt(m_NodeNum);
	    ret=m_TreeNodeObject.createTreeNodeObject(Utils.addStrings(m_TreeNodeObject.getLocation(),".",node.label()),node);
	    if (m_TagName.length()>0)
		findNextNode();
	    else
		m_NodeNum++;
	}
	return ret;
    }
    /**
     * This method resets the enumeration to the beginning - you can now iterate over the list again
     */
    public void reset(){
	m_NodeNum=0;
	findNextNode();
    }
    /**
     * this method winds m_NodeNum onto the next matching element - or beyond the end of the list for the end of enumeration
     */
    void findNextNode(){
	Vector nodes=m_TreeNodeObject.getNode().getItems();
	if (nodes!=null)
	    for (;m_NodeNum<nodes.size();m_NodeNum++){
		TreeNode node=(TreeNode)nodes.elementAt(m_NodeNum);
		if (node.label().equals(m_TagName)){
		    break;
		}
	    }
    }
}
